/*************************************************************************
*
*     Program:        Poland_Seats.do
*     Author:         Joshua Tucker
*     Purpose:        To calculate seats for simulated parliaments
*     Date:           4/30/00
*     Version:        Stata 6.0
**************************************************************************/


set more off
clear
set mem 80m


*PREPARING DATA AND OPENING LOG FILE

use "poland.dta", clear
capture log using "1000 Sejms.log", replace


*GENERATING NECESSARY Variables for Program

gen num = _N				/*num = # of observations before any sims*/
gen ord = _n				/*ORIGINAL ORDER -- sort by this to keep everything in same order*/

*e = expected value (mean of simulations) for vote percentage
*v = calculated vote (e * total votes in region)
*s = seats for party
*t = dummies for threshold (5%, 7%, 8%)
*c = dummy for coalition 

*t`k'f = cleared five percent threshold
*t`k's = cleared seven percent threshold
*t`k'e = cleared eight percent threshold


local parties sld psl ud bwr up kpn kld oth

	while "`parties'" ~="" {
	gettoken k parties : parties

 			gen t`k'f = 0
 			gen t`k's = 0
 			gen t`k'e = 0

		
		local i 1
 	
		while `i' <= 54 {
	

 			gen s`k'`i' = 0
			gen e`k'`i' = 0
 			gen v`k'`i' = 0
		

		local i = `i' + 1
		}			
	}

drop soth*

*CREATING OTHER CATEGORY;
#delimit ;

gen other = 100 - (apsld + appsl + apud + apbbwr + apup + apkpn + apkld);



*RUNNING COMPOSITIONAL DATA REGRESSION;

*LOGIT TRANSFORMATION;

tlogit apsld lpsld appsl lppsl apud lpud apbbwr lpbbwr
apup lpup apkpn lpkpn apkld lpkld, base(other) percent;

*REGRESSION;
set matsize 250;

log on;
estsimp sureg (lpsld pun993 wctsl wcpcse pcstfm pcstnf pcinfm pcpmmt pc209 pco65 pchged)
(lppsl pun993 wctsl wcpcse pcstfm pcstnf pcinfm pcpmmt pc209 pco65 pchged)
(lpud pun993 wctsl wcpcse pcstfm pcstnf pcinfm pcpmmt pc209 pco65 pchged)
(lpbbwr pun993 wctsl wcpcse pcstfm pcstnf pcinfm pcpmmt pc209 pco65 pchged)
(lpup pun993 wctsl wcpcse pcstfm pcstnf pcinfm pcpmmt pc209 pco65 pchged)
(lpkpn pun993 wctsl wcpcse pcstfm pcstnf pcinfm pcpmmt pc209 pco65 pchged)
(lpkld pun993 wctsl wcpcse pcstfm pcstnf pcinfm pcpmmt pc209 pco65 pchged), small;


#delimit cr

global i 1					/*region*/
local n = num[1]			/*number of regions (ie. orginal number of obs*/

*GET 1000 EXPECTED VALUES FOR EACH REGION


while $i <= `n' {

    sort ord	
    setx [$i]
	 setx wctsl (wctsl[$i] + 5)
	 setx pun993 (pun993[$i] - 2.3)
	 setx pcstnf (pcstnf[$i] - 10)


    simqi, msims(1000) tfunc(logiti) genev(pred1 pred2 pred3 pred4 pred5 pred6 pred7)
	sort ord
			
		local parties sld psl ud bwr up kpn kld
		local j 1

			while "`parties'" ~="" {
			gettoken k parties : parties

			qui replace e`k'$i = pred`j' 
	
			local j = `j' + 1
			}


*CREATE PREDICTED OTHER VOTE IN EACH REGION

	replace eoth$i = 1 - (esld$i + epsl$i + eud$i + ebwr$i + eup$i +  ekpn$i + ekld$i)





	drop pred*

	di _con "Region " $i " "
	global i = $i + 1
} 


save "1000 Sejms", replace



*DEALING WITH SPLITTING WARSAW AND KATOWICE INTO SEPARATE DISTRICTS

sort ord
replace district = "1" in 1
replace district = "2" in 50
replace district = "15" in 7
replace district = "16" in 51
replace district = "17" in 52

*Need to give correct # of seats to new districts

replace seats = 17 in 1			/*This will be home of new district 1*/
replace seats = 8 in 50			/*This will be home of new district 2*/
replace seats = 10 in 7			/*This will be home of new district 15*/
replace seats = 17 in 51		/*This will be home of new district 16*/
replace seats = 14 in 52		/*This will be home of new district 17*/


*DISTRIBUTING VOTES BASED ON ESTIMATED VOTE PROPORTION

local parties sld psl ud bwr up kpn kld oth


	while "`parties'" ~="" {
	gettoken k parties : parties

		local i 1 	
		while `i' <= 49 {

		
			qui replace v`k'`i' = (e`k'`i' * totvotes[`i'])	/*CALCULATES VOTES FROM EXPECTED PERCENTAGES FOR ALL 1000 SIMULATIONS OF REGION 1*/

		local i = `i' + 1		
		}
	
*FIXING WARSAW AND KATOWICE

	qui replace v`k'50 = (v`k'1 * .252)			/*District 50 is real district 2, has real # of seats*/
 	qui replace v`k'1 = (v`k'1 * .748)			/*District 1 is now vote in real district 1*/


	qui replace v`k'51 = (v`k'7 * .392)			/*District 51 is now real 16*/
	qui replace v`k'52 = (v`k'7 * .318)			/*District 52 is now real 17*/
	qui replace v`k'7 = (v`k'7 * .290)			/*District 7 is now real 7*/


*CREATING COALITION MARKERS

	qui gen c`k' = 0

	}



*FIXING ANYTHING ELSE BEFORE GOING INTO SIMULATED SEJMS

*LABEL SLD AS COALITION

replace csld = 1
gen disnum=real(district)

*DEAL WITH GERMAN SEATS BY REDUCING AVAILABLE SEATS IN DISTRICTS

replace seats = seats - 3 if disnum == 30
replace seats = seats - 1 if disnum == 17


replace seats = 69 in 53		/* SETS NUMBER OF NATIONAL SEATS */

* SETTING ALL SEAT MARKERS TO 0*

local parties sld psl ud bwr up kpn kld

while "`parties'" ~="" {
	gettoken k parties : parties
	local i 1
	while `i' <= 53 {

		qui replace s`k'`i' = 0	
		local i = `i' + 1
		}
	}

*SETTING UP LOOP FOR EACH OF THE 1000 SEJMs

sort ord
local i 1
while `i' <= 1000 {

*FIRST, CHECK TO SEE WHO PASSESS THE THRESHOLD

local parties sld psl ud bwr up kpn kld

	while "`parties'" ~="" {
		gettoken k parties : parties
 	

*GO GET VOTE IN EACH REGION FOR PARTY K

		local vottot 0
		local j 1
		while `j' <= 52 {

			local vottot = `vottot' + v`k'`j'[`i']			
			local j = `j' + 1
			}

	
		qui replace v`k'53 = `vottot' in `i'		/*For the ith simulation, party k has total vote of vottot, which we are putting in v`k'53*/
					





*NOW CHECK WHETHER PARTY K PASSED THRESHOLD NATIONALLY
*RECORD IN NATPER FOR THAT VERSION OF THE SEJM 

		local natper = v`k'53[`i']/13796227 
 		qui replace t`k'f = 1 if `natper' >= .05 in `i'
 		qui replace t`k'f = 0 if `natper' < .05 in `i'
		qui replace t`k's = 1 if `natper' >= .07 in `i'
		qui replace t`k's = 0 if `natper' < .07 in `i'
		qui replace t`k'e = 1 if `natper' >= .08 in `i'
		qui replace t`k'e = 0 if `natper' < .08 in `i'		
		
	}






*NOW DISTRIBUTE THE SEATS FOR THE "Ith" SEJM


*STARTING THE DISTRIBUTION OF REGIONAL SEATS

*CHECK TO SEE WHO PASSES THE THRESHOLD

local parties sld psl ud bwr up kpn kld
 
while "`parties'" ~= "" {
   gettoken k parties : parties
	
	local j 1 			/*For coalitions*/
	local pr 1			/*For parties*/

	if t`k'f > .5 in `i' {local pr = `pr' + 1}
	if c`k' == 0 in `i' {local pr = `pr' + 1}  
	if `pr' == 3 { local pass `pass' `k' }

	if t`k'e > .5 in `i' {local j = `j' + 1}
	if c`k' == 1 in `i' {local j = `j' + 1}  
	if `j' == 3 { local pass `pass' `k' }

	}

tokenize `pass'
local npart : word count `pass'






sort ord

*SET UP VARIABLES FOR SEAT DISTRIBUTION LOOPS

local t 1 				/*DISTICTS*/

*DISTRICT LOOP: t stands for disTrict

while `t' <= 52 {


*SEAT LOOP

	local s 1
	while `s' <= seats[`t'] {
		
*AVERAGE LOOP
		local p 1
		while `p' <= `npart'  {
					

		local av`p' = (v``p''`t'[`i']/(1 + s``p''`t'[`i'])) 	   

*THIS IS VOTE FOR PARTY P REGION T (eg. vsld12) SIMULATION 1 (so obs 1)

			local p = `p' + 1
			}


*COMPARISON LOOP
		local m 1
		while `m' <= `npart' {
			local rank`m' 0	      		/*START RANK AT 0*/



		local n 1
		while `n' <= `npart' {
			
if `av`m'' < `av`n'' { local rank`m' = `rank`m'' + 1 }   /*INCREASES IF LESS THAN SOMEONE ELSE*/

				local n = `n' + 1
				}


*IF NOT LESS THAN ANYONE, YOU WIN THAT SEAT


if `rank`m'' == 0 { qui replace s``m''`t' in `i' = s``m''`t'[`i'] + 1 }	/*IF YOU ARE NOT LESS THAN SOMEONE ELSE, YOU GET SEAT */


*TO CHECK IF WORKING, INCLUDE NEXT LINE

* if `rank`m'' == 0 {di "In Region  `t' simulation `i' the seat #  `s'  goes to party  ``m'' "}
					
			local m = `m' + 1
			}


		local s = `s' + 1
   		}
		

     	local t = `t' + 1


   	}





*DISTRIBUTE NATIONAL SEATS
*SEE WHO PASSESS THE NATIONAL THRESHOLD


local parties sld psl ud bwr up kpn kld
 
while "`parties'" ~= "" {
   gettoken k parties : parties
	
	if t`k's > .5 in `i' { local passnat `passnat' `k' }

		}

tokenize `passnat'
local npart : word count `passnat'






sort ord


*SEAT LOOP
	
	local t 53
	local s 1
	while `s' <= seats[`t'] {
		
*AVERAGE LOOP
		local p 1
		while `p' <= `npart'  {
					

		local av`p' = (v``p''`t'[`i']/(1 + s``p''`t'[`i']))   

			local p = `p' + 1
			}


*COMPARISON LOOP
		local m 1
		while `m' <= `npart' {
			local rank`m' 0	      		/*START RANK AT 0*/



		local n 1
		while `n' <= `npart' {
			
if `av`m'' < `av`n'' { local rank`m' = `rank`m'' + 1 }   /*INCREASES IF LESS THAN SOMEONE ELSE*/

				local n = `n' + 1
				}




if `rank`m'' == 0 { qui replace s``m''`t' in `i' = s``m''`t'[`i'] + 1 }	/*IF YOU ARE NOT LESS THAN SOMEONE ELSE, YOU GET SEAT */


* if `rank`m'' == 0 {di "In Region  `t' simulation `i' the seat #  `s'  goes to party  ``m'' "}
					
			local m = `m' + 1
			}


		local s = `s' + 1
   		}
		







*GET SEAT TOTALS: National and OVERALL

	local parties sld psl ud bwr up kpn kld

	while "`parties'" ~="" {
	gettoken k parties : parties

	local sn = s`k'53[`i']
*	dis "Party `k' received `sn' seats in the national list in simulation `i' " 
	 

		local seattot 0
		local j 1
		while `j' <= 53 {

			local seattot = `seattot' + s`k'`j'[`i']
			
			local j = `j' + 1
			}

	qui replace s`k'54 = `seattot' in `i'		/*For the ith simulation, party k has total seats of seattot, which we are putting in s`k'54*/
 	
	local st = s`k'54 in `i'		

*	dis "Party `k' received `st' seats overall in simulation `i' " 



	}

local passnat " "
local pass " "	

dis "Simulation `i' completed"


*NOTE: WE HAVE NOW COMPLETED DISTRIBUTING ALL SEATS FOR Ith PARLIAMENT

local i = `i' + 1
}		


*DISPLAY SUMMARY STATISTICS ACROSS ALL THE FINDINGS


	local parties sld psl ud bwr up kpn kld

	while "`parties'" ~="" {
	gettoken k parties : parties

	qui sum s`k'54, detail
	
	dis "With All shifted Party `k' has a median of " r(p50) " seats"
	
	_pctile s`k'54, p(2.5,97.5)

	dis "With All shifted Party `k' had a range of " r(r1) " to " r(r2) "."	

	}



*GENERATE VARIABLES
*GAPS: gslud = gap sld - ud; gslref = gap sld - reformists

gen gslud = ssld54 - sud54
gen gslref = ssld54 - (sud54 + sbwr54 + skld54)


*COALITIONS: cslps = SLD + PSL; cref = reformist; crefps = reformist+ psl

gen cslps = ssld54 + spsl54
gen cref = sud54 + sbwr54 + skld54
gen crefps = cref + spsl54



	local parties gslud gslref cslps cref crefps

	while "`parties'" ~="" {
	gettoken k parties : parties


	qui sum `k', detail
	
	dis "Gap/Coalition `k' has a median of " r(p50) " seats"
	
	_pctile `k', p(2.5,97.5)

	dis "Gap coalition `k' had a range of " r(r1) " to " r(r2) "."	

}





save "1000 Sejms COMPLETE", replace


log close
set more on







